<template>
  <div>
    <header class="header">
      <div class="header-content">
        <div class="header-left">
          <img
            src="./img/dog.svg"
            width="80px"
            height="80px"
            alt="可爱的小狗"
          />
          <ul>
            <li>
              <router-link to="/"
                ><a-icon type="home" theme="filled" />&nbsp;首页</router-link
              >
            </li>
            <li><router-link to="/adopt">免费领养</router-link></li>
          </ul>
        </div>
        <div class="header-center">
          <div class="input-container">
            <input
              type="search"
              class="input"
              v-model="keyWord"
              placeholder="请输入想要领养的宠物吧！"
              @keyup.enter="search"
            />
            <span class="icon" @click="search"><a-icon type="search" /></span>
          </div>
        </div>
        <div class="header-right">
          <div class="avatar-container">
            <div class="avatar-container-inside">
              <a-avatar
                class="avatar"
                size="large"
                icon="user"
                :src="$store.state.avatarPath"
              />
              <ul class="avatar-nav">
                <li>
                  {{ isLogin ? $store.state.nickname : "您目前还未登录" }}
                </li>
                <li><router-link to="/userInfo">个人中心</router-link></li>
                <li>
                  <router-link to="/fosterage/myFosterage"
                    >我的寄养</router-link
                  >
                </li>
                <li @click="exit()" v-if="isLogin">
                  <router-link to="/login">退出登录</router-link>
                </li>
                <li v-else>
                  <router-link to="/login">去登录</router-link>
                </li>
              </ul>
            </div>
          </div>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <span
            v-if="isLogin"
            style="
              width: 150px;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
            "
            >你好！{{ $store.state.nickname }}</span
          >
          <!-- <button class="login-btn">登录</button> -->
          <router-link to="/login" v-else
            ><button class="login-btn">登录</button></router-link
          >
        </div>
      </div>
    </header>
    
    <router-view></router-view>
    <Footer></Footer>
    <a-back-top />
  </div>
</template>

<script>
import requests from "./js/requests.js";
import { Avatar, Icon, BackTop } from "ant-design-vue";
import Footer from "../Footer/Footer.vue";

export default {
  name: "Header",
  components: {
    AAvatar: Avatar,
    AIcon: Icon,
    Footer,
    ABackTop: BackTop,
  },
  data() {
    return {
      isLogin: false,
      keyWord: "",
    };
  },
  mounted() {
    // 每次加载该组件时更新到最新的头像
    let token = localStorage.getItem("token");
    if (token) {
      requests.getUserInfo().then((res) => {
        if (res.code == 200) {
          this.isLogin = true;
          this.$store.dispatch("setNickname", res.data.nickname);
          if (res.data.avatar) {
            this.$store.dispatch("setAvatarPath", res.data.avatar);
          } else {
            this.$store.dispatch("setAvatarPath", "/img/defaultAvatar.webp");
          }
        } else {
          this.$store.dispatch("setAvatarPath", "/img/loginAvatar.png");
        }
      });
    } else {
      this.$store.dispatch("setAvatarPath", "/img/loginAvatar.png");
    }
  },
  methods: {
    //   点击搜索按钮
    search() {
      if (this.$router.history.current.path != "/adopt") {
        this.$store.dispatch("setKeyWord", this.keyWord);
        this.$router.push({ path: "/adopt" });
      }
      this.$bus.$emit("search", this.keyWord);
    },
    // 退出登录
    exit() {
      localStorage.removeItem("token");
    },
  },
};
</script>

<style scoped>
.ant-back-top {
  bottom: 150px;
  right: 20px;
}
.header {
  width: 100%;
  height: 80px;
  /* margin-bottom: 5px; */
  box-shadow: 0px 1px 5px 0px rgb(220, 220, 220);
}

.header-content {
  width: 1440px;
  height: 100%;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  /* background-color: rgb(255, 246, 197); */
}
/* 头部左边样式 */
.header-left {
  width: 500px;
  height: 100%;
  display: flex;
  /* background-color: aqua */
}

.header-left ul {
  width: 300px;
  height: 80px;
  display: flex;
  align-items: center;
}

.header-left ul li {
  margin: 0 15px;
}

.header-left ul li a {
  display: inline-block;
  padding: 3px 10px;
  color: black;
  border-radius: 2px;
}

.header-left ul li a:hover {
  background-color: darkgray;
  color: rgb(0, 47, 255);
}
/* 头部中间样式 */
.header-center {
  width: 440px;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  /* background-color: rgb(72, 255, 0) */
}

.input-container {
  width: 100%;
  height: 40px;
  position: relative;
}

.input {
  width: 100%;
  height: 40px;
  padding: 0 40px 0 30px;
  border-radius: 20px;
  border: 0;
  outline: none;
  color: black;
  background-color: rgb(240, 240, 240);
  transition: 0.5s;
}
.input-container .icon {
  position: absolute;
  top: 4px;
  right: 12px;
  font-size: 24px;
  color: rgb(151, 151, 151);
  transition: 0.2s;
  cursor: pointer;
}

.input-container .icon:hover {
  transform: scale(1.3, 1.3);
  color: rgb(80, 80, 80);
}

.input:focus {
  box-shadow: 0 0 5px 0 rgb(200, 200, 200);
}

.input::placeholder {
  color: rgba(121, 121, 121, 0.5);
}

/* 头部右边样式 */
.header-right {
  width: 470px;
  height: 100%;
  padding-right: 30px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.avatar-container {
  height: 40px;
  width: 40px;
  position: relative;
}

.avatar-container-inside {
  width: 140px;
  height: 200px;
  position: absolute;
  top: 20px;
  left: -50px;
  background-color: brown;
  z-index: 99;
  visibility: hidden;
}

.avatar-container .avatar {
  position: absolute;
  top: -20px;
  left: 50px;
  z-index: 101;
  transition: 0.3s;
  visibility: visible;
}

.avatar-container .avatar-nav {
  width: 140px;
  height: 200px;
  padding-top: 40px;
  position: absolute;
  top: 20px;
  border-radius: 10px;
  box-shadow: 0 5px 5px rgb(0, 0, 0, 0.3);
  background-color: rgb(255, 255, 255);
  z-index: 100;
  opacity: 0;
  visibility: hidden;
  overflow: hidden;
  transition: 0.3s;
}

.avatar-container .avatar-nav li {
  width: 100%;
  height: 40px;
  text-align: center;
  line-height: 40px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  transition: 0.2s;
  /* background-color: #ec9e18; */
}

.avatar-container .avatar-nav li:hover {
  background-color: rgb(0, 0, 0, 0.2);
}

.avatar-container .avatar-nav li a {
  display: inline-block;
  width: 100%;
  height: 100%;
}

.avatar-container .avatar-nav {
  font-weight: 800;
}

.avatar-container .avatar-nav li:first-child {
  padding-left: 15px;
  padding-right: 15px;
  color: black;
}

.avatar-container .avatar-nav li:first-child:hover {
  background-color: inherit;
}

.avatar-container .avatar:hover {
  transform: scale(1.5, 1.5);
}

.avatar-container-inside:hover .avatar {
  transform: scale(1.5, 1.5);
}

.avatar-container-inside:hover .avatar-nav {
  top: 0;
  opacity: 1;
  visibility: visible;
}

.login-btn {
  width: 50px;
  height: 30px;
  margin-left: 30px;
  margin-right: 70px;
  border: none;
  border-radius: 5px;
  color: aliceblue;
  /* line-height: 25px; */
  background-color: #ec9e18;
  transition: 0.3s;
}

.login-btn:hover {
  box-shadow: 0 5px 10px rgb(0, 0, 0, 0.5);
}

</style>